<?php
namespace app\beautiful\controller\admin\setting;
use app\BaseController;
use app\common\helper\printer\feie\FeiePrinter;
use app\common\helper\printer\Yly\YlyPrinter;
use app\beautiful\logic\printer\PrinterLogic;
use app\beautiful\model\PrinterModel;
use app\beautiful\model\setting\PrinterAppModel;

class Printer extends BaseController{
    public function showPrinterSetting()
    {
        if(null === $setting = PrinterAppModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0]
            ])->find()){
            return $this->success([]);
        }

        $detail=[
            'app_type'=>'yly',
            'client_id'=>$setting->client_id,
            'client_secret'=>$setting->client_secret,
            'user'=>$setting->user,
            'ukey'=>$setting->ukey,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function savePrinterSetting()
    {
        $app_type=$this->request->param('app_type');
        
        if(empty($app_type)) return $this->error('参数错误');
        if(!in_array($app_type,['yly','feie'])) return $this->error('参数错误');
        
        if($app_type=='yly'){
            $client_id=$this->request->param('client_id');
            $client_secret=$this->request->param('client_secret');

            if(empty($client_id)) return $this->error('请输入应用id');
            if(empty($client_secret)) return $this->error('请输入应用私钥');
            try {
                if(null === $printerApp = PrinterAppModel::where([
                        ['shop_id','=',$this->request->shopId],
                        ['is_delete','=',0]
                    ])->find()){

                    $printerApp=new PrinterAppModel();
                    $printerApp->shop_id=$this->request->shopId;

                }else{
                    if( $printerApp->client_id==$client_id  && $printerApp->client_secret==$client_secret){
                        return $this->success('绑定成功');
                    }
                }
                $ylyPrinter=new YlyPrinter();
                $res=$ylyPrinter->OauthPrinter($client_id,$client_secret);
                $access_token=$res->access_token;
                $refresh_token=$res->refresh_token;
                $expires_in=$res->expires_in;
                $printerApp->access_token=$access_token;
                $printerApp->refresh_token=$refresh_token;
                $printerApp->expires_in=$expires_in;
                $printerApp->client_id=$client_id;
                $printerApp->client_secret=$client_secret;
                $printerApp->save();
                return $this->success('绑定成功');
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }
        }else{
            $user=$this->request->param('user');
            $ukey=$this->request->param('ukey');
            if(empty($user)){
                return $this->error('请输入USER');
            }
            if(empty($ukey)){
                return $this->error('请输入UKEY');
            }
            if(null === $printerApp = PrinterAppModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0]
                ])->find()){

                $printerApp=new PrinterAppModel();
                $printerApp->shop_id=$this->request->shopId;
            }
            $printerApp->user=$user;
            $printerApp->ukey=$ukey;
            $printerApp->save();
            return $this->success('添加成功');
        }
        

    }


    public function getPrinterList()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
        ];

        $total=PrinterModel::where($where)->count();
        $list=PrinterModel::where($where)->order(['add_time'=>'desc'])
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->append(['add_time_format','app_type_mean'])
            ->toArray();
        $return=[
            'total'=>$total,
            'list'=>$list
        ];
        return $this->success($return);
    }

    public function showPrinter()
    {
        $return=[];
        $printer_id=$this->request->param('printer_id/d',0);
        if($printer_id==0)return $this->success($return);
        if(null === $printer = PrinterModel::find($printer_id)) return $this->error('打印机不存在');
        $data=[
            'app_type'=>$printer->app_type,
            'printer_name'=>$printer->printer_name,
            'machine_code'=>$printer->machine_code,
            'msign'=>$printer->msign,
            'print_sn'=>$printer->print_sn,
            'print_key'=>$printer->print_key,
            'jurisdiction'=>$printer->jurisdiction,
        ];
        $return['data']=$data;
        return $this->success($return);
    }

    public function bindYlyPrinter()
    {
        $machine_code=$this->request->param('machine_code');
        $msign=$this->request->param('msign');
        $printer_name=$this->request->param('printer_name');
        $printer_id=$this->request->param('printer_id/d',0);
        $jurisdiction=$this->request->param('jurisdiction/a');

        if(empty($machine_code))  return $this->error('请输入终端号');
        if(empty($msign)) return $this->error('请输入终端密钥');
        if(empty($printer_name))  return $this->error('请输入打印机别名');
        if(empty($jurisdiction)) return $this->error('请给一点权限吧');
        try{
            if($printer_id==0){
                $printer= new PrinterModel();
                $printer->shop_id=$this->request->shopId;
            }else{

                if(null === $printer = PrinterModel::find($printer_id)) return $this->error('打印机不存在');
                if($printer->shop_id != $this->request->shopId)  return $this->error('打印机不存在');

                if($printer->msign == $msign && $printer->machine_code == $machine_code && $printer->app_type == 'yly'){
                    $printer->printer_name=$printer_name;
                    $printer->jurisdiction=$jurisdiction;
                    $printer->save();
                    return $this->success('操作成功');
                }

            }
            if(PrinterModel::where([
                ['shop_id','=',$this->request->shopId],
                ['app_type','=','yly'],
                ['machine_code','=',$machine_code],
                ['msign','=',$msign]
            ])->find()){
                return $this->error('打印机已已存在');
            }

            if(null === $setting = PrinterAppModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0]
                ])->find()){
               $setting=[];
            }else{
                $setting->toArray();
            }
            $ylyPrinter= new YlyPrinter($setting);
            $ylyPrinter->addPrinter($machine_code,$msign,$printer_name);
            $printer->app_type='yly';
            $printer->printer_name=$printer_name;
            $printer->jurisdiction=$jurisdiction;
            $printer->machine_code=$machine_code;
            $printer->msign=$msign;
            $printer->save();
            return $this->success('操作成功');

        }catch(\Exception $e){
            return $this->error($e->getMessage());
        }
    }

    public function bindFeiePrinter()
    {
        $print_sn=$this->request->param('print_sn');
        $print_key=$this->request->param('print_key');
        $printer_name=$this->request->param('printer_name');
        $printer_id=$this->request->param('printer_id/d',0);
        $jurisdiction=$this->request->param('jurisdiction/a');
        if(empty($print_sn)) return $this->error('请输入打印机编号');
        if(empty($print_key)) return $this->error('请输入识别码');
        if(empty($printer_name))  return $this->error('请输入打印机别名');
        if(empty($jurisdiction)) return $this->error('请给一点权限吧');
        try{
            if($printer_id==0){
                $printer= new PrinterModel();
                $printer->shop_id=$this->request->shopId;
            }else{
                if(null === $printer = PrinterModel::find($printer_id)) return $this->error('打印机不存在');
                if($printer->shop_id != $this->request->shopId)  return $this->error('打印机不存在');
                if($printer->print_sn == $print_sn && $printer->print_key == $print_key && $printer->app_type == 'feie'){
                    $printer->printer_name=$printer_name;
                    $printer->jurisdiction=$jurisdiction;
                    $printer->save();
                    return $this->success('操作成功');
                }
            }

            if(PrinterModel::where([
                ['shop_id','=',$this->request->shopId],
                ['app_type','=','feie'],
                ['print_sn','=',$print_sn],
                ['print_key','=',$print_key]
            ])->find()){
                return $this->error('打印机已已存在');
            }

            if(null === $setting = PrinterAppModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0]
                ])->find()){
                $setting=[];
            }else{
                $setting->toArray();
            }
            $feiePrinter= new FeiePrinter($setting);
            $feiePrinter->addPrinter($print_sn,$print_key,$printer_name);
            $printer->app_type='feie';
            $printer->printer_name=$printer_name;
            $printer->jurisdiction=$jurisdiction;
            $printer->print_sn=$print_sn;
            $printer->print_key=$print_key;
            $printer->save();
            return $this->success('操作成功');

        }catch(\Exception $e){
            return $this->error($e->getMessage());
        }
    }
}
